columns=[[
    {
        field : 'ck',
        checkbox : true
    },{
        field:'id',
        title:'序号',
        align : 'center',
        width:100,
        hidden : true
    },{
        field:'userName',
        title:'姓名',
        align : 'center',
        width:100
    },{
          field:'sex',
          title:'性别',
          align : 'center',
          width:100
    },{
        field:'evpAccount',
        title:'帐号',
        align : 'center',
        width:200
    },
    {
        field:'deptName',
        title:'部门',
        align : 'center',
        width:200
    },
    {
        field:'addr',
        title:'地址',
        align : 'center',
        width:200,
        formatter: function(value,row,index){
            return '<span title="'+value+'">'+value+'</span>'
        }
    }
]];

$(function(){
    $('#userDataGrid').datagrid({
        url:'/eUser/list',
        method:'get',
        columns: columns,
        rownumbers: true,
        pagePosition: 'bottom',
        pagination: true,
        pageList: [10,20,30,40,50],
        idField: 'id',
        pageSize:10,
        pageNumber: 1,
        striped: true,
        singleSelect:true
    });

    var p = $('#userDataGrid').datagrid().datagrid('getPager');

    p.pagination({
        beforePageText: '第',
        afterPageText:'共{pages}页',
        displayMsg: '当前显示 {from} 到 {to}条记录 ,共{total}条记录'
    });

    $('#userNameCBBox').combobox({
        url:'/eUser/list',
        method:'get',
        valueField:'userName',
        textField:'userName',
        hasDownArrow: false,
        panelHeight: 'auto',
        onChange:function(newValue,oldValue){
             if (newValue != null && newValue !== "") {
                  $('#userNameCBBox').combobox('reload','/eUser/list/combobox?userName='+newValue);
             }
             if (newValue ==="") {
                 $('#userNameCBBox').combobox('loadData', {});
                 $("#userNameCBBox").combobox('clear').combobox('loadData', {});
               }
        }
    });

    $('#accountCBBox').combobox({
        url:'/eUser/list',
        method:'get',
        valueField:'evpAccount',
        textField:'evpAccount',
        hasDownArrow: false,
        panelHeight: 'auto',
        onChange:function(newValue,oldValue){
             if (newValue != null && newValue !== "") {
                  $('#accountCBBox').combobox('reload','/eUser/list/combobox?evpAccount='+newValue);
             }
             if (newValue ==="") {
                 $('#accountCBBox').combobox('loadData', {});
                 $("#accountCBBox").combobox('clear').combobox('loadData', {});
               }
        }
    });

    // 添加区域窗口
    $('#addUserWindow').window({
        title : '添加用户',
        width : 550,
        modal : true,
        shadow : true,
        closed : true,
        height : 450,
        resizable : false
    });

    // 编辑区域窗口
    $('#editUserWindow').window({
        title : '编辑用户',
        width : 550,
        modal : true,
        shadow : true,
        closed : true,
        height : 450,
        resizable : false
    });

    // 部门树窗口
    $('#deptWindow').window({
        title : '请选择部门',
        width : 250,
        shadow : true,
        closed : true,
        height : 500,
        resizable : false
    });

    // 使窗口居中显示
    $("#addUserWindow").window('center');
    $("#editUserWindow").window('center');
});

function searchUserList(){
    var userName=$('#userNameCBBox').combobox('getValue');
    var evpAccount=$('#accountCBBox').combobox('getValue');
    $('#userDataGrid').datagrid('load',{
    	userName: userName,
    	evpAccount: evpAccount
    });


}

function addUserWindowOpen(){
    $('#addUserWindow').window('open');
}

function addUserWindowClose(){
    $('#addUserWindow').window('close');
}

/*
    打开添加用户
*/
function addUserWindow(){
    addUserWindowOpen();
}

/*
    打开编辑用户
*/
function editUserWindow(){
    var userRow=$('#userDataGrid').datagrid('getSelected');
    $("#editUserForm").form('load',userRow);
     $('#editUserWindow').window('open');
}

/*
    提交添加的用户信息
*/
function submitUserForm(){
    var checkResult=false;

    checkResult=checkValidateForm($('#addUserForm'));

    if(checkResult){
        var data={
            userName:$("#userName").val(),
            evpAccount:$("#evpAccount").val(),
            evpPassword:$("#evpPassword").val(),
            deptId:$("#deptId").val(),
            phone:$("#phone").val(),
            addr:$("#addr").val(),
            sex:$("#sex").combobox('getValue')
        };

        apiUserManageSubmitUserForm(JSON.stringify(data),function(res){
            if(res.status==200){
                var tip="<span style='height=20px;line-height:20px;'><img src='js/jquery-easyui-1.7.0/themes/icons/ok.png'/><span style='margin-left:10px'>"+res.message+"</span></span>"
                messager('成功',tip,4000,'info');

                $('#userDataGrid').datagrid('reload');
                addUserWindowClose();
            }else{
                var tip="<span style='height=20px;line-height:20px;'><img src='js/jquery-easyui-1.7.0/themes/icons/no.png'/><span style='margin-left:10px'>"+res.message+"</span></span>"
                messager('失败',tip,4000,'info');
            }

        });
    }
}

/*
    提交编辑的用户信息
*/
function submitEditUserForm(){
    var checkResult=false;

    checkResult=checkValidateForm($('#editUserWindow'));

    if(checkResult){
        var data={
            id:$("#id").val(),
            userName:$("#editUserName").val(),
            evpAccount:$("#editEvpAccount").val(),
            deptId:$("#editDeptId").val(),
            phone:$("#editPhone").val(),
            addr:$("#editAddr").val(),
            sex:$("#editSex").combobox('getValue')
        };

        apiUserManageSubmitEditUserForm(JSON.stringify(data),function(res){
            if(res.status==200){
                var tip=getMessagerTip("ok",res.message);
                messager('成功',tip,4000,'info');

                $('#userDataGrid').datagrid('reload');
                $("#editUserWindow").window('close');
            }else{
                var tip=getMessagerTip("no",res.message);
                messager('失败',tip,4000,'info');
            }
        });
    }
}

function remoceUser(){
    $.messager.confirm('确认','您确认想要删除该用户吗？',function(r){
        if (r){
            var userRow=$('#userDataGrid').datagrid('getSelected');
                            var userId=userRow.id;
                            apiUserManageRemoveUser(userId,function(res){
                                if(res.status==200){
                                    var tip=getMessagerTip("ok",res.message);
                                    messager('成功',tip,4000,'info');

                                    $('#userDataGrid').datagrid('reload');
                                }else{
                                    var tip=getMessagerTip("no",res.message);
                                    messager('失败',tip,4000,'info');
                                }
                            });
        }
    });
}

function clearUserForm(){
    $('#addUserForm').form('clear');
}

function clearEditUserForm(){
    $('#editUserWindow').form('clear');
}

/*
    为新增的用户选择部门
    调取部门树
*/
function searchDeptTree(){
    $("#deptWindow").window('open');

    $('#deptTree').tree({
        url: '/eDepart/tree',
        method: 'get',
        loadFilter: function(data){
               if (data.d){
                   return data.d;
               } else {
                   return data;
               }
           },
        onClick: function(node){
               clickDeptTreeNode(this,node);
        }

    });
}

/*
    单击部门树某节点
*/
function clickDeptTreeNode(ele,node){
    var treeNode=$('#deptTree').tree('getSelected');

    if(treeNode!=null){
        $("#deptName").textbox('setValue',treeNode.text);
        $("#deptId").textbox('setValue',treeNode.id);
        $("#editDdeptName").textbox('setValue',treeNode.text);
        $("#editDeptId").textbox('setValue',treeNode.id);
    }
}